package com.njlime.cms.controller.sys;

import com.njlime.cms.entities.Label;
import com.njlime.cms.service.sys.LabelService;
import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.exception.ParamException;
import com.njlime.core.helper.PageBean;
import com.njlime.core.web.validation.annotation.support.ValidateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 标签控制器
 * Created by Dean on 2017/8/10.
 */
@Controller
@RequestMapping(value = "/cms/label")
public class LabelController extends BaseController {

    @Resource
    LabelService labelService;

    //--------------------控制器-------------------------

    /**
     * 跳转标签管理页面
     * @return
     */
    @RequestMapping(value = "labelManage")
    public ModelAndView infoManage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/content/label/index");
        return modelAndView;
    }


    //------------------功能--------------------------


    /**
     * 获取详情
     * @param id
     * @return
     */
    @RequestMapping(value = "info", method = RequestMethod.GET)
    @ResponseBody
    public String info(int id) {
        Label label = labelService.findOne(id);
        checkDataPermission(label.getUnit_id(), label.getCreator());
        return toJsonSuccessData(labelService.findOne(id));
    }

    /**
     * 添加
     * @param label
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public String add(Label label) {
        try{
            ValidateService.valid(label);
        }catch (Exception e){
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION,e.getMessage());
        }
        label.setCreator(getLoginUser().getId());
        label.setUnit_id(getLoginUserUnitId());
        int count = labelService.save(label);
        if (count > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION,"添加失败");
        }
    }

    /**
     * 编辑
     * @param label
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(Label label) {
        Label ori = labelService.findOne(label.getId());
        checkDataPermission(ori.getUnit_id(), ori.getCreator());
        label.setUpdater(getLoginUser().getId());
        label.setUpdate_time(new Date());
        int count = labelService.editLabel(label);
        if (count > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION,"编辑失败");
        }
    }

    /**
     * 删除
     * @param request
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public String del(HttpServletRequest request,Integer id) {
        Label ori = labelService.findOne(id);
        checkDataPermission(ori.getUnit_id(), ori.getCreator());
        int count = labelService.del(id);
        if (count > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION,"删除失败");
        }
    }

    /**
     * 分页列表
     * @param pageBean
     * @param label
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(PageBean pageBean, Label label) {
        if(pageBean == null){
            pageBean = new PageBean();
        }
        return toJsonListData(labelService.list(pageBean, label));
    }

}
